package com.home.base;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothManager;
import android.bluetooth.le.BluetoothLeScanner;
import android.bluetooth.le.ScanCallback;
import android.bluetooth.le.ScanResult;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Build;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;

import androidx.fragment.app.FragmentActivity;

import com.common.BaseActivity;
import com.common.listener.ListenerManager;
import com.common.listener.ReceiveDataListenerManager;
import com.common.uitl.SharePersistent;
import com.home.activity.main.MainActivity_BLE;
import com.home.activity.main.MainActivity_LIKE;
import com.home.adapter.BluetoothDataModel;
import com.home.bean.SceneBean;
import com.home.constant.CommonConstant;
import com.home.constant.Constant;
import com.home.db.GroupDevice;
import com.home.net.NetConnectBle;
import com.home.service.BluetoothLeServiceSingle;
import com.home.service.MyServiceConenction;
import com.mo.demo.MyApp;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

@SuppressLint("MissingPermission")
public class Test {

    public static final String NAME_START_LED = "LED";
    Handler h = new Handler();
    private BluetoothLeServiceSingle mBluetoothLeService;
    BaseActivity activity;

    public Test(BaseActivity activity) {
        this.activity = activity;
        initBLE(activity);
    }


    private ScanCallback scanCallback = new ScanCallback() { // from class: com.home.fragment.service.ServicesFragment.16
        @Override // android.bluetooth.le.ScanCallback
        public void onScanResult(int i, ScanResult scanResult) {
            super.onScanResult(i, scanResult);
            BluetoothDevice device = scanResult.getDevice();
            if (device.getName() == null || !device.getName().startsWith(NAME_START_LED) || MyApp.getApp().getBleDevices().contains(device)) {
                return;
            }
            MyApp.getApp().getBleDevices().add(device);
            updateNewFindDevice();
            if (MyApp.getApp().isAuto()) {
                if (conectHandler != null) {
                    conectHandler.sendEmptyMessage(10000);
                    return;
                }
                return;
            }
            ArrayList arrayList = new ArrayList();
            Iterator<BluetoothDataModel> it = MyApp.getApp().getManmualBleDevices().iterator();
            while (it.hasNext()) {
                arrayList.add(it.next().getDevice());
            }
            if (!arrayList.contains(device)) {
                BluetoothDataModel bluetoothDataModel = new BluetoothDataModel();
                bluetoothDataModel.setDevice(device);
                MyApp.getApp().getManmualBleDevices().add(bluetoothDataModel);
            }
            refreshApp(device);
        }
    };
    private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() { // from class: com.home.fragment.service.ServicesFragment.17
        @Override // android.bluetooth.BluetoothAdapter.LeScanCallback
        public void onLeScan(final BluetoothDevice bluetoothDevice, int i, byte[] bArr) {
            h.post(new Runnable() { // from class: com.home.fragment.service.ServicesFragment.17.1
                @SuppressLint("MissingPermission")
                @Override // java.lang.Runnable
                public void run() {
                    String name;
                    if (bluetoothDevice == null || MyApp.getApp().getBleDevices().contains(bluetoothDevice) || bluetoothDevice.getName() == null || (name = bluetoothDevice.getName()) == null || !name.startsWith(BluetoothLeServiceSingle.NAME_START_LED)) {
                        return;
                    }
                    MyApp.getApp().getBleDevices().add(bluetoothDevice);
                    updateNewFindDevice();
                    if (MyApp.getApp().isAuto()) {
                        if (conectHandler != null) {
                            conectHandler.sendEmptyMessage(10000);
                        }
                    } else {
                        BluetoothDataModel bluetoothDataModel = new BluetoothDataModel();
                        bluetoothDataModel.setDevice(bluetoothDevice);
                        MyApp.getApp().getManmualBleDevices().add(bluetoothDataModel);
                    }
                }
            });
        }
    };
    private boolean booleanCanStart = false;
    private Boolean hasOpenBle = true;


    private Handler conectHandler = new Handler() { // from class: com.home.fragment.service.ServicesFragment.18
        @Override // android.os.Handler
        public void handleMessage(Message message) {
            int i = message.what;
            if (i != 1000) {
                if (i == 10000 && !booleanCanStart) {
                    booleanCanStart = true;
                    startConnectDevices();
                    return;
                }
                return;
            }
        }
    };

    protected void refreshBluetoothAdapter() {
        BluetoothManager bluetoothManager = (BluetoothManager) activity.getSystemService("bluetooth");
        this.bluetoothManager = bluetoothManager;
        mBluetoothAdapter = bluetoothManager.getAdapter();
        if (Build.VERSION.SDK_INT >= 23) {
            this.bleScanner = mBluetoothAdapter.getBluetoothLeScanner();
        }
    }

    public void clearList() {
        if (MyApp.getApp().getBleGattMap().size() > 0) {
            for (final String str : MyApp.getApp().getBleGattMap().keySet()) {
                new Thread(new Runnable() { // from class: com.home.fragment.service.ServicesFragment.21
                    @Override // java.lang.Runnable
                    public void run() {
                        MyApp.getApp().clearBleGatMap(str);
                    }
                }).start();
                try {
                    Thread.sleep(100L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            MyApp.getApp().getBleGattMap().clear();
        }
        MyApp.getApp().getBleDevices().clear();
        MyApp.getApp().getHashMapConnect().clear();
        MyApp.getApp().getManmualBleDevices().clear();
        updateNewFindDevice();
    }

    private Handler refreshBLEHandler;
    Runnable refreshBLERunnable;
    private static String DISCONNECTED_BLE_ADDRESS = null;


    public void refreshBLE() {
        if (this.refreshBLEHandler == null) {
            this.refreshBLEHandler = new Handler();
        }
        if (this.refreshBLERunnable == null) {
            this.refreshBLERunnable = new Runnable() { // from class: com.home.fragment.service.ServicesFragment.22
                @Override // java.lang.Runnable
                public void run() {
                    refreshBleDevices();
                    if (refreshBLEHandler != null) {
                        refreshBLEHandler.postDelayed(refreshBLERunnable, 8000L);
                    }
                    if (MyApp.getApp().getHashMapConnect().containsKey(DISCONNECTED_BLE_ADDRESS)) {
                        refreshBLEHandler.removeCallbacks(refreshBLERunnable);
                    }
                }
            };
        }
        Handler handler = this.refreshBLEHandler;
        if (handler != null) {
            handler.postDelayed(this.refreshBLERunnable, 100L);
        }
    }


    private final BroadcastReceiver mGattUpdateReceiver = new BroadcastReceiver() { // from class: com.home.fragment.service.ServicesFragment.20
        public int DEFAULT_VALUE_BULUETOOTH = 1000;

        @Override // android.content.BroadcastReceiver
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            intent.getStringExtra("name");
            String stringExtra = intent.getStringExtra(GroupDevice.ADDRESSNUM);
            if ("android.bluetooth.adapter.action.STATE_CHANGED".equals(action)) {
                switch (intent.getIntExtra("android.bluetooth.adapter.extra.STATE", this.DEFAULT_VALUE_BULUETOOTH)) {
                    case 10:
                        hasOpenBle = false;
                        return;
                    case 11:
                        hasOpenBle = false;
                        return;
                    case 12:
                        hasOpenBle = true;
                        refreshBluetoothAdapter();
                        refreshBleDevices();
                        return;
                    case 13:
                        hasOpenBle = false;
                        clearList();
                        return;
                    default:
                        return;
                }
            }
            if (BluetoothLeServiceSingle.ACTION_GATT_CONNECTED.equals(action)) {
                MyApp.getApp().getHashMapConnect().put(stringExtra, true);
                updateNewFindDevice();
                return;
            }
            if (BluetoothLeServiceSingle.ACTION_GATT_DISCONNECTED.equals(action)) {
                DISCONNECTED_BLE_ADDRESS = stringExtra;
                MyApp.getApp().getHashMapConnect().put(stringExtra, false);
                MyApp.getApp().getHashMapConnect().remove(stringExtra);
                MyApp.getApp().getBleGattMap().remove(stringExtra);
                Iterator<BluetoothDevice> it = MyApp.getApp().getBleDevices().iterator();
                while (true) {
                    if (!it.hasNext()) {
                        break;
                    }
                    BluetoothDevice next = it.next();
                    if (next.getAddress().equals(stringExtra)) {
                        MyApp.getApp().getBleDevices().remove(next);
                        break;
                    }
                }
                Iterator<BluetoothDataModel> it2 = MyApp.getApp().getManmualBleDevices().iterator();
                while (true) {
                    if (!it2.hasNext()) {
                        break;
                    }
                    BluetoothDataModel next2 = it2.next();
                    if (next2.getDevice().getAddress().equals(stringExtra)) {
                        MyApp.getApp().getManmualBleDevices().remove(next2);
                        break;
                    }
                }
                updateNewFindDevice();
                hashMapLock.remove(stringExtra);
                if (MyApp.getApp().isAuto() && refreshBLEHandler == null) {
                    refreshBLE();
                    return;
                }
                return;
            }
            if (BluetoothLeServiceSingle.ACTION_GATT_SERVICES_DISCOVERED.equals(action)) {
                if (refreshBLEHandler != null) {
                    refreshBLEHandler.removeCallbacks(refreshBLERunnable);
                    refreshBLEHandler = null;
                }
                BluetoothGatt bluetoothGatt = mBluetoothLeService.getBluetoothGatt();
                if (bluetoothGatt.getDevice().getName().contains("LEDDMX-02-")) {
                    bluetoothGatt.requestMtu(500);
                }
                MyApp.getApp().getBleGattMap().put(stringExtra, bluetoothGatt);
                hashMapLock.put(stringExtra, true);
                ListenerManager.getInstance().sendBroadCast(Constant.SmartTimeNowSet);

                send(null, Color.RED, true, true);
                return;
            }
            if (BluetoothLeServiceSingle.ACTION_DATA_AVAILABLE.equals(action)) {
                String stringExtra2 = intent.getStringExtra(BluetoothLeServiceSingle.EXTRA_DATA);
                if (context != null) {
                    String perference = SharePersistent.getPerference(context, Constant.Activity);
                    if (stringExtra2.contains("1A 05") || stringExtra2.contains("1a 05")) {
                        return;
                    }
                    if (perference.equalsIgnoreCase(Constant.CurrentQueryActivity)) {
                        String perference2 = SharePersistent.getPerference(context, Constant.CurrentQueryActivity);
                        if (perference2.length() > 0) {
                            SharePersistent.savePerference(context, Constant.CurrentQueryActivity, perference2 + "" + stringExtra2);
                            return;
                        }
                        SharePersistent.savePerference(context, Constant.CurrentQueryActivity, stringExtra2);
                        return;
                    }
                    if (perference.equalsIgnoreCase(Constant.TimingQueryActivity)) {
                        String perference3 = SharePersistent.getPerference(context, Constant.TimingQueryActivity);
                        if (perference3.contains(stringExtra2)) {
                            return;
                        }
                        if (perference3.length() > 0) {
                            SharePersistent.savePerference(context, Constant.TimingQueryActivity, perference3 + " " + stringExtra2);
                            return;
                        }
                        SharePersistent.savePerference(context, Constant.TimingQueryActivity, stringExtra2);
                        return;
                    }
                    if (perference.equalsIgnoreCase(Constant.MaterialLibraryActivity)) {
                        ReceiveDataListenerManager.getInstance().sendBroadCast(Constant.MaterialLibraryActivity, stringExtra2);
                        return;
                    }
                    if (perference.equalsIgnoreCase(Constant.DMX02ModeFragment)) {
                        ReceiveDataListenerManager.getInstance().sendBroadCast(Constant.DMX02ModeFragment, stringExtra2);
                    } else if (perference.equalsIgnoreCase(Constant.DMX02TextFragment)) {
                        ReceiveDataListenerManager.getInstance().sendBroadCast(Constant.DMX02TextFragment, stringExtra2);
                    } else if (perference.equalsIgnoreCase(Constant.DMX02GraffitFragment)) {
                        ReceiveDataListenerManager.getInstance().sendBroadCast(Constant.DMX02GraffitFragment, stringExtra2);
                    }
                }
            }
        }
    };

    public void updateNewFindDevice() {
        ListenerManager.getInstance().sendBroadCast(Constant.UpdateNewFindDevice);
    }

    public void goToMain(String str) {
        if (str == null) {
            return;
        }
        MyApp.getApp().setSceneBean(str.replaceAll("-", ""));
        SharePersistent.savePerference(activity, Constant.CUSTOM_DIY_APPKEY, str);
        if (str.contains(CommonConstant.LEDBLE) || str.contains("LEDDMX-00-") || str.contains("LEDDMX-01-") || str.contains("LEDDMX-03-") || str.contains("LEDCAR-00-") || str.contains("LEDCAR-01-") || str.equalsIgnoreCase(CommonConstant.LEDSMART) || str.equalsIgnoreCase(CommonConstant.LEDSTAGE) || str.equalsIgnoreCase(CommonConstant.LEDSUN)) {
            Intent intent = new Intent(activity, (Class<?>) MainActivity_BLE.class);
            intent.putExtra("scene", str);
            activity.startActivity(intent);
        } else if (str.equalsIgnoreCase(CommonConstant.LEDLIGHT)) {
            Intent intent2 = new Intent(activity, (Class<?>) MainActivity_BLE.class);
            intent2.putExtra("scene", str);
            activity.startActivity(intent2);
        } else if (str.equalsIgnoreCase(CommonConstant.LEDLIKE)) {
            Intent intent3 = new Intent(activity, (Class<?>) MainActivity_LIKE.class);
            intent3.putExtra("scene", str);
            activity.startActivity(intent3);
        }
    }


    private volatile HashMap<String, Boolean> hashMapLock;
    private BluetoothLeScanner bleScanner;
    private BluetoothManager bluetoothManager;
    private static BluetoothAdapter mBluetoothAdapter;

    private MyServiceConenction myServiceConenction;

    private static IntentFilter makeGattUpdateIntentFilter() {
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BluetoothLeServiceSingle.ACTION_GATT_CONNECTED);
        intentFilter.addAction(BluetoothLeServiceSingle.ACTION_GATT_DISCONNECTED);
        intentFilter.addAction(BluetoothLeServiceSingle.ACTION_GATT_SERVICES_DISCOVERED);
        intentFilter.addAction(BluetoothLeServiceSingle.ACTION_DATA_AVAILABLE);
        intentFilter.addAction("android.bluetooth.adapter.action.STATE_CHANGED");
        return intentFilter;
    }


    private void initBLE(Activity mActivity) {
        this.hashMapLock = new HashMap<>();
        mActivity.registerReceiver(this.mGattUpdateReceiver, makeGattUpdateIntentFilter());
        BluetoothManager bluetoothManager = (BluetoothManager) mActivity.getSystemService("bluetooth");
        this.bluetoothManager = bluetoothManager;
        mBluetoothAdapter = bluetoothManager.getAdapter();
        if (Build.VERSION.SDK_INT >= 23) {//android 6.0
            this.bleScanner = mBluetoothAdapter.getBluetoothLeScanner();
        }
        MyServiceConenction myServiceConenction = new MyServiceConenction();
        this.myServiceConenction = myServiceConenction;
        myServiceConenction.setServiceConnectListener(new MyServiceConenction.ServiceConnectListener() { // from class: com.home.fragment.service.ServicesFragment.13
            @Override // com.home.service.MyServiceConenction.ServiceConnectListener
            public void onDisConnect(ComponentName componentName) {
            }

            @Override // com.home.service.MyServiceConenction.ServiceConnectListener
            public void onConnect(ComponentName componentName, IBinder iBinder, BluetoothLeServiceSingle bluetoothLeServiceSingle) {
                mBluetoothLeService = bluetoothLeServiceSingle;
                mBluetoothLeService.initialize();
                startLeScan();
            }
        });
        mActivity.bindService(new Intent(mActivity, (Class<?>) BluetoothLeServiceSingle.class), this.myServiceConenction, 1);
    }

    public static BluetoothAdapter getBluetoothAdapter() {
        BluetoothAdapter bluetoothAdapter = mBluetoothAdapter;
        if (bluetoothAdapter != null) {
            return bluetoothAdapter;
        }
        return null;
    }

    protected void refreshBleDevices() {
        startLeScan();
    }

    protected void startLeScan() {
        if (activity != null) {
            if (this.hasOpenBle.booleanValue() && getBluetoothAdapter().isEnabled()) {
                if (Build.VERSION.SDK_INT < 23) {
                    BluetoothAdapter bluetoothAdapter2 = mBluetoothAdapter;
                    if (bluetoothAdapter2 != null) {
                        bluetoothAdapter2.startLeScan(this.mLeScanCallback);
                    }
                } else {
                    BluetoothLeScanner bluetoothLeScanner2 = this.bleScanner;
                    if (bluetoothLeScanner2 != null) {
                        bluetoothLeScanner2.startScan(this.scanCallback);
                    }
                }
                final Handler handler2 = new Handler();
                handler2.postDelayed(new Runnable() { // from class: com.home.fragment.service.ServicesFragment.15
                    @Override // java.lang.Runnable
                    public void run() {
                        stopLeScan();
                        handler2.removeCallbacks(this);
                    }
                }, 6000L);
            }
        }
    }

    protected void stopLeScan() {
        ScanCallback scanCallback;
        BluetoothAdapter.LeScanCallback leScanCallback;
        if (this.hasOpenBle.booleanValue()) {
            if (getBluetoothAdapter().isEnabled()) {
                if (Build.VERSION.SDK_INT < 23) {
                    if (this.bleScanner != null && (leScanCallback = this.mLeScanCallback) != null) {
                        mBluetoothAdapter.stopLeScan(leScanCallback);
                    }
                } else {
                    BluetoothLeScanner bluetoothLeScanner = this.bleScanner;
                    if (bluetoothLeScanner != null && (scanCallback = this.scanCallback) != null) {
                        bluetoothLeScanner.stopScan(scanCallback);
                    }
                }
            }
            if (MyApp.getApp().getBleDevices().size() > 0) {
                for (int i = 0; i < MyApp.getApp().getBleDevices().size(); i++) {
                    if (!MyApp.getApp().getBleGattMap().containsKey(MyApp.getApp().getBleDevices().get(i).getAddress())) {
                        startConnectDevices();
                        return;
                    }
                }
            } else {
                h.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        startLeScan();
                    }
                }, 1000);
            }
        }
    }

    public void startConnectDevices() {
        System.out.println("mBluetoothLeService:" + this.mBluetoothLeService);
        new Thread(new Runnable() { // from class: com.home.fragment.service.ServicesFragment.19
            @Override // java.lang.Runnable
            public void run() {
                while (mBluetoothLeService != null) {
                    try {
                        Iterator<BluetoothDevice> it = MyApp.getApp().getBleDevices().iterator();
                        while (it.hasNext()) {
                            BluetoothDevice next = it.next();
                            String address = next.getAddress();
                            String name = next.getName();
                            if (!MyApp.getApp().getBleGattMap().containsKey(address) && mBluetoothLeService != null) {
                                if (MyApp.getApp().isCanConnect()) {
                                    mBluetoothLeService.connect(address, name);
                                    hashMapLock.put(address, false);
                                }
                                do {
                                    sleep(200);
                                } while (!((Boolean) hashMapLock.getOrDefault(address, true)).booleanValue());
                            }
                            sleep(200);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    sleep(200);
                }
            }
        }).start();
    }

    private void sleep(long time) {
        try {
            Thread.sleep(time);
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    private ArrayList<String> serviceStrArr = new ArrayList<>();
    private ArrayList<SceneBean> sceneBeans = new ArrayList<>();

    public void refreshApp(BluetoothDevice bluetoothDevice) {
        this.serviceStrArr.clear();
        if (this.sceneBeans == null || activity == null) {
            return;
        }
        int i = 0;
        if (bluetoothDevice.getName().startsWith(BluetoothLeServiceSingle.LED) || bluetoothDevice.getName().startsWith(BluetoothLeServiceSingle.LED_)) {
            SceneBean sceneBean = new SceneBean("LED BLE", CommonConstant.LEDBLE_01, null, 0);
            for (int i2 = 0; i2 < this.sceneBeans.size(); i2++) {
                this.serviceStrArr.add(this.sceneBeans.get(i2).getSerice());
            }
            if (!this.serviceStrArr.contains(CommonConstant.LEDBLE_01)) {
                this.sceneBeans.add(sceneBean);
            }
            SceneBean sceneBean2 = new SceneBean("LED DMX", "LEDDMX-01-", null, 0);
            while (i < this.sceneBeans.size()) {
                this.serviceStrArr.add(this.sceneBeans.get(i).getSerice());
                i++;
            }
            if (!this.serviceStrArr.contains("LEDDMX-01-")) {
                this.sceneBeans.add(sceneBean2);
            }
        } else if (bluetoothDevice.getName().startsWith(CommonConstant.LEDBLE)) {
            if (bluetoothDevice.getName().startsWith(CommonConstant.LEDBLE_00)) {
                SceneBean sceneBean3 = new SceneBean("LED BLE", CommonConstant.LEDBLE_00, null, 0);
                while (i < this.sceneBeans.size()) {
                    this.serviceStrArr.add(this.sceneBeans.get(i).getSerice());
                    i++;
                }
                if (!this.serviceStrArr.contains(CommonConstant.LEDBLE_00)) {
                    this.sceneBeans.add(sceneBean3);
                }
            } else {
                SceneBean sceneBean4 = new SceneBean("LED BLE", CommonConstant.LEDBLE_01, null, 0);
                while (i < this.sceneBeans.size()) {
                    this.serviceStrArr.add(this.sceneBeans.get(i).getSerice());
                    i++;
                }
                if (!this.serviceStrArr.contains(CommonConstant.LEDBLE_01)) {
                    this.sceneBeans.add(sceneBean4);
                }
            }
        } else if (bluetoothDevice.getName().contains(BluetoothLeServiceSingle.LEDDMX)) {
            if (bluetoothDevice.getName().contains("LEDDMX-00-")) {
                SceneBean sceneBean5 = new SceneBean("LED DMX", "LEDDMX-00-", null, 0);
                while (i < this.sceneBeans.size()) {
                    this.serviceStrArr.add(this.sceneBeans.get(i).getSerice());
                    i++;
                }
                if (!this.serviceStrArr.contains("LEDDMX-00-")) {
                    this.sceneBeans.add(sceneBean5);
                }
            } else if (bluetoothDevice.getName().startsWith("LEDDMX-02-")) {
                if (activity != null) {
                    SceneBean sceneBean6 = new SceneBean("LED DMX", "LEDDMX-02-", null, 0);
                    while (i < this.sceneBeans.size()) {
                        this.serviceStrArr.add(this.sceneBeans.get(i).getSerice());
                        i++;
                    }
                    if (!this.serviceStrArr.contains("LEDDMX-02-")) {
                        this.sceneBeans.add(sceneBean6);
                    }
                }
            } else if (!bluetoothDevice.getName().startsWith("LEDDMX-03-")) {
                SceneBean sceneBean7 = new SceneBean("LED DMX", "LEDDMX-01-", null, 0);
                while (i < this.sceneBeans.size()) {
                    this.serviceStrArr.add(this.sceneBeans.get(i).getSerice());
                    i++;
                }
                if (!this.serviceStrArr.contains("LEDDMX-01-")) {
                    this.sceneBeans.add(sceneBean7);
                }
            } else if (activity != null) {
                SceneBean sceneBean8 = new SceneBean("LED DMX", "LEDDMX-03-", null, 0);
                while (i < this.sceneBeans.size()) {
                    this.serviceStrArr.add(this.sceneBeans.get(i).getSerice());
                    i++;
                }
                if (!this.serviceStrArr.contains("LEDDMX-03-")) {
                    this.sceneBeans.add(sceneBean8);
                }
            }
        } else if (bluetoothDevice.getName().startsWith("LEDCAR-00-")) {
            SceneBean sceneBean9 = new SceneBean("LEDCAR00", "LEDCAR-00-", null, 0);
            while (i < this.sceneBeans.size()) {
                this.serviceStrArr.add(this.sceneBeans.get(i).getSerice());
                i++;
            }
            if (!this.serviceStrArr.contains("LEDCAR-00-")) {
                this.sceneBeans.add(sceneBean9);
            }
        } else if (!bluetoothDevice.getName().startsWith("LEDCAR-01-")) {
            String[] strArr = {CommonConstant.LEDBLE, CommonConstant.LEDSMART, CommonConstant.LEDSTAGE, CommonConstant.LEDSUN, CommonConstant.LEDLIKE};
            String[] strArr2 = {CommonConstant.LEDBLE, CommonConstant.LEDSMART, CommonConstant.LEDSTAGE, CommonConstant.LEDSUN, CommonConstant.LEDLIKE};
            String str = bluetoothDevice.getName().split("-")[0];
            for (int i3 = 0; i3 < 5; i3++) {
                if (str.equalsIgnoreCase(strArr[i3])) {
                    SceneBean sceneBean10 = new SceneBean(strArr2[i3], strArr[i3], null, 0);
                    for (int i4 = 0; i4 < this.sceneBeans.size(); i4++) {
                        this.serviceStrArr.add(this.sceneBeans.get(i4).getSerice());
                    }
                    if (!this.serviceStrArr.contains(strArr[i3])) {
                        this.sceneBeans.add(sceneBean10);
                    }
                }
            }
        } else if (activity != null) {
            SceneBean sceneBean11 = new SceneBean("LEDCAR01", "LEDCAR-01-", null, 0);
            while (i < this.sceneBeans.size()) {
                this.serviceStrArr.add(this.sceneBeans.get(i).getSerice());
                i++;
            }
            if (!this.serviceStrArr.contains("LEDCAR-01-")) {
                this.sceneBeans.add(sceneBean11);
            }
        }
        this.conectHandler.sendEmptyMessage(1000);
        SharePersistent.setObjectValue(activity, CommonConstant.KEY_HOME_SCENES, this.sceneBeans);
    }

    public void onDestroy() {
        MyServiceConenction myServiceConenction;
        BroadcastReceiver broadcastReceiver;
        try {
            ListenerManager.getInstance().unRegisterListener(activity);
            ReceiveDataListenerManager.getInstance().unRegisterListener(activity);
            FragmentActivity fragmentActivity = activity;
            if (fragmentActivity != null && (broadcastReceiver = this.mGattUpdateReceiver) != null) {
                fragmentActivity.unregisterReceiver(broadcastReceiver);
            }
            FragmentActivity fragmentActivity2 = activity;
            if (fragmentActivity2 != null && (myServiceConenction = this.myServiceConenction) != null) {
                fragmentActivity2.unbindService(myServiceConenction);
            }
            ArrayList<SceneBean> arrayList = this.sceneBeans;
            if (arrayList == null || arrayList.size() <= 0) {
                SharePersistent.setObjectValue(activity, CommonConstant.APP_MODE, null);
            }
            Handler handler = this.refreshBLEHandler;
            if (handler != null) {
                handler.removeCallbacks(this.refreshBLERunnable);
                this.refreshBLEHandler = null;
            }
        } catch (Exception unused) {
        }
    }

    public static int[] getRGB(int i) {
        return new int[]{Color.red(i), Color.green(i), Color.blue(i), Color.alpha(i)};
    }


    public void send(SceneBean sceneBean, int i, boolean z, boolean z2) {
        int[] rgb = getRGB(i);
        if (z) {
            if (sceneBean != null && (sceneBean.getName().equalsIgnoreCase(CommonConstant.LEDSTAGE) || sceneBean.getName().equalsIgnoreCase(CommonConstant.LEDLIGHT))) {
                //    setStageLightRgb(rgb[0], rgb[1], rgb[2], 255, true);
            } else {
                int[] color = getRGB(i);
                setBleRgb(color[0], color[1], color[2], true);
            }
        }
    }

    public void setBleRgb(int i, int i2, int i3, boolean z) {

        setRegModeNoInterval(136, false);

//        if (z) {
//            NetConnectBle.getInstance().setBleRgb(i, i2, i3);
//        }
    }

    public void setRegModeNoInterval(int i, boolean z) {
        NetConnectBle.getInstance().setRgbMode(i, CommonConstant.LEDBLE_01, z);
    }
}
